﻿
namespace TPTecnicas.MessageProcessor.Actions
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using TPTecnicas.MessageProcessor.Common;
    using System.Text.RegularExpressions;
    using TPTecnicas.MessageProcessor.Business;
    using System.ComponentModel.Composition;
    
   class GenerarTicket : Action
    {
        [ImportingConstructor]
        public GenerarTicket([Import("context")]IUniversityContext context)
            : base(context)
        {
        }
        public override void Execute(IMessage message)
        {
            var regex = new Regex(@"^\[CONSULTA-(?<tipoConsulta>\w+)\] (?<tema>.*)$");
            var match = regex.Match(message.Subject);
            var tipoConsulta = match.Groups["tipoConsulta"].Value;
            var tema = match.Groups["tema"].Value;

            if (!Context.TicketRepository.TicketCreado(tema))
            {
                Context.TicketRepository.CrearTicket(tema, message.Body, "SIN ASIGNAR", "");
            }
            else
            {
                Context.TicketRepository.AgregarRespuestaTicket(tema, message.Body);
                Context.TicketRepository.CambiarEstadoTicket(tema, "PENDIENTE");
            }
        }
    }
}